﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using JXC.Core;
using JXC.Models;
using JXC.UI.Common;

namespace JXC.UI.BaseData
{
    public partial class GYSForm : XtraForm
    {
        private APIClient client=new APIClient();
        public GYSForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            var frm=new GYSEditForm();
            frm.mg = gridControl1;
            frm.ShowDialog();
        }

        private void GYSForm_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = client.GetGHSList();
            gridView1.OptionsBehavior.Editable=false;
            gridView1.BestFitColumns();
        }

        private void groupControl2_Paint(object sender, PaintEventArgs e)
        {

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            var frm = new GYSEditForm();
            frm.IsEdit = true;
            frm.data = (ghsxx)gridView1.GetRow(gridView1.FocusedRowHandle);
            frm.mg= gridControl1;
            frm.ShowDialog();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            client.DeleteGHS(new List<ghsxx>() { (ghsxx)gridView1.GetRow(gridView1.FocusedRowHandle) });
            var data = client.GetGHSList();
            gridControl1.DataSource = data;
        }

        private void simpleButton10_Click(object sender, EventArgs e)
        {
            var data = client.GetGHSList();
            if (!string.IsNullOrEmpty(textEdit2.Text))
            {
                data=data.Where(p => p.GHSName.Equals(textEdit2.Text)).ToList();
            }

            gridControl1.DataSource = data;
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            gridControl1.ExportData();
        }

        private void simpleButton9_Click(object sender, EventArgs e)
        {
            var p = (XtraTabPage)this.Parent;
            if (p != null)
            {
                var tab = (XtraTabControl)p.Parent;
                tab.TabPages.Remove(p);
                tab.SelectedTabPageIndex = tab.TabPages.Count - 1;
            }
            this.Close();
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            var frm = new GHSImportDataForm();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                var data = client.GetGHSList();
                gridControl1.DataSource = data;
            }
        }
    }
}
